package com.deve.pig.service.merchant.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import my.utils.Pagination;

import org.springframework.stereotype.Service;

import com.deve.pig.common.BusinessData;
import com.deve.pig.mapper.MerchantGoodsMapper;
import com.deve.pig.model.MerchantGoods;
import com.deve.pig.model.MerchantGoodsExample;
import com.deve.pig.model.MerchantGoodsExample.Criteria;
import com.deve.pig.service.merchant.IMerchantGoodsService;

@Service("merchantGoodsService")
public class MerchantGoodsServiceImpl extends BusinessData implements IMerchantGoodsService {

    @Resource
    private MerchantGoodsMapper merchantGoodsMapper;

    @Override
    public int add(MerchantGoods entity) {
        return merchantGoodsMapper.insert(entity);
    }

    @Override
    public int delete(MerchantGoodsExample example) {
        return merchantGoodsMapper.deleteByExample(example);
    }

    @Override
    public int delete(int id) {
        return merchantGoodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(MerchantGoods entity) {
        return merchantGoodsMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public Pagination findPagination(Map<String, Object> queryMap, int currentPage, int pageSize) {
        MerchantGoodsExample merchantGoodsExample = new MerchantGoodsExample();
        merchantGoodsExample.setOrderByClause("is_top desc, update_time desc");
        Criteria criteria = merchantGoodsExample.createCriteria();
        criteria.andMerchantIdEqualTo(Integer.parseInt((String) queryMap.get("merchantId")));
        // 设置搜索条件参数
        if (queryMap != null) {
            if (queryMap.containsKey("name")) {
                criteria.andNameLike("%" + (String) queryMap.get("name") + "%");
            }
            if (queryMap.containsKey("merchantId")) {
                criteria.andMerchantIdEqualTo(Integer.parseInt((String)queryMap.get("merchantId")));
            }
        }
        // 设置分页参数
        merchantGoodsExample.setPageSize(pageSize);
        merchantGoodsExample.setStartIndex((currentPage - 1) * pageSize);
        List<MerchantGoods> items = merchantGoodsMapper.selectByExample(merchantGoodsExample);
        int totalCount = (int) merchantGoodsMapper.countByExample(merchantGoodsExample);
        return new Pagination(pageSize, currentPage, totalCount, items);
    }

    @Override
    public MerchantGoods findById(int id) {
        return merchantGoodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<MerchantGoods> findAll() {
        return merchantGoodsMapper.selectByExample(null);
    }

    @Override
    public List<MerchantGoods> findByCriteria(MerchantGoodsExample criteria) {
        return merchantGoodsMapper.selectByExample(criteria);
    }

    @Override
    @SuppressWarnings("unchecked")
    public LinkedHashMap<String, List> getExportData() {
        LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
        map.put("SheetName_1", findAll());
        map.put("SheetName_2", findAll());
        map.put("SheetName_3", findAll());
        map.put("SheetName_N", findAll());
        return map;
    }

    @Override
    public int countByExample(MerchantGoodsExample example) {
        return merchantGoodsMapper.countByExample(example);
    }
}
